//
//  MainTableViewController.m
//  project
//
//  Created by 张振宇 on 15/10/26.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainTableViewController.h"
#import "OtherTableViewCell.h"
#import "StrategyDetailsViewController.h"
#import "FMDatabase.h"

@interface MainTableViewController ()

@property(nonatomic,strong)FMDatabase *dataBase;
@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation MainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[OtherTableViewCell class] forCellReuseIdentifier:@"OtherTableViewCell1"];
    self.tableView.rowHeight = 200;
    
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(9, 1, 1)firstObject]stringByAppendingPathComponent:@"project.db"];
    _dataBase = [FMDatabase databaseWithPath:filepath];
    [_dataBase open];
    
    _array = [NSMutableArray array];
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from AllLove"];
    while ([resultSet next])
    {
        NSString *loveID = [resultSet stringForColumn:@"loveid"];
        if (loveID == nil)
        {
            
        }
        else
        {
            [self.array addObject:loveID];
        }
        
    }


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataArr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailsViewController *strategDetailsVC = [[StrategyDetailsViewController alloc]init];
    strategDetailsVC.contentId = ((OtherTableViewModel *)_dataArr[indexPath.row]).Myid;
    strategDetailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationC pushViewController:strategDetailsVC animated:YES];
    strategDetailsVC.hidesBottomBarWhenPushed = NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OtherTableViewCell1" forIndexPath:indexPath];
    //cell.textLabel.text = @"fds";
    cell.configure = _dataArr[indexPath.row];
    // Configure the cell...
    cell.selectionStyle = NO;
    cell.layer.cornerRadius = 20;
    cell.layer.masksToBounds = YES;
    for (NSString *str in self.array)
    {
        if ([[NSString stringWithFormat:@"%@",cell.configure.Myid] isEqualToString:str])
        {
            [cell.btn setImage:[UIImage imageNamed:@"iconfont-icon503-2"] forState:(UIControlStateNormal)];
        }
    }
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
